home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Orlando_1993 / Devcon93.1 / IFF / newiff39 / modules / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  13.0 KB  |  459 lines

  1. /* screen.c - 2.0 screen module for Display
  2.  * based on scdemo, oscandemo, looki
  3.  * 10/91 - added public screen support by checking user screen tags
  4.  *         for SA_PubScreen and setting screen public if found.
  5.  * 09/92 - added V39 BestFit call for modefallback, and usermodeid support
  6.  */
  7.  
  8. /*
  9. Copyright (c) 1989, 1990 Commodore-Amiga, Inc.
  10.  
  11. Executables based on this information may be used in software
  12. for Commodore Amiga computers. All other rights reserved.
  13. This information is provided "as is"; no warranties are made.
  14. All use is at your own risk, and no liability or responsibility
  15. is assumed.
  16. */
  17.  
  18. #define INTUI_V36_NAMES_ONLY
  19.  
  20. #include "iffp/ilbmapp.h"
  21.  
  22. BOOL   VideoControlTags(struct ColorMap *,ULONG tags, ...);
  23.  
  24. extern struct Library *GfxBase;
  25. extern struct Library *IntuitionBase;
  26.  
  27. struct TextAttr SafeFont = { (UBYTE *) "topaz.font", 8, 0, 0, };
  28. UWORD  penarray[] = {~0};
  29.  
  30. /* default new window if none supplied in ilbm->nw */
  31. struct   NewWindow      defnw = {
  32.    0, 0,                                  /* LeftEdge and TopEdge */
  33.    0, 0,                                /* Width and Height */
  34.    -1, -1,                                /* DetailPen and BlockPen */
  35.    IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS, /* IDCMP Flags with Flags below */
  36.    WFLG_BACKDROP | WFLG_BORDERLESS |
  37.    WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
  38.    WFLG_ACTIVATE | WFLG_RMBTRAP,
  39.    NULL, NULL,                            /* Gadget and Image pointers */
  40.    NULL,                                  /* Title string */
  41.    NULL,                                  /* Screen ptr null till opened */
  42.    NULL,                                  /* BitMap pointer */
  43.    50, 20,                                /* MinWidth and MinHeight */
  44.    0 , 0,                                 /* MaxWidth and MaxHeight */
  45.    CUSTOMSCREEN                           /* Type of window */
  46.    };
  47.  
  48.  
  49. /* opendisplay - passed ILBMInfo, dimensions, modeID
  50.  *
  51.  *    Attempts to open correct 2.0 modeID screen and window,
  52.  *    else an old 1.3 mode screen and window.
  53.  *
  54.  * Returns *window or NULL.
  55.  */
  56.  
  57. struct Window *opendisplay(struct ILBMInfo *ilbm,
  58.                SHORT wide, SHORT high, SHORT deep,
  59.                ULONG mode)
  60.     {
  61.     struct NewWindow newwin, *nw;
  62.  
  63.     closedisplay(ilbm);
  64.     if(ilbm->scr = openidscreen(ilbm, wide, high, deep, mode))
  65.     {
  66.     nw = &newwin;
  67.     if(ilbm->windef) *nw = *(ilbm->windef);
  68.     else *nw = *(&defnw);
  69.     nw->Screen    = ilbm->scr;
  70.  
  71.     D(bug("sizes: scr= %ld x %ld  passed= %ld x %ld\n",
  72.         ilbm->scr->Width,ilbm->scr->Height,wide,high)); 
  73.  
  74.     nw->Width    = wide;
  75.     nw->Height    = high;
  76.     if (!(ilbm->win = OpenWindow(nw)))
  77.         {
  78.         closedisplay(ilbm);
  79.         D(bug("Failed to open window."));
  80.         }
  81.     else
  82.         {
  83.         if(ilbm->win->Flags & WFLG_BACKDROP)
  84.         {
  85.         ShowTitle(ilbm->scr, FALSE);
  86.         ilbm->TBState = FALSE;
  87.         }
  88.         }
  89.     }
  90.  
  91.     if(ilbm->scr)    /* nulled out by closedisplay if OpenWindow failed */
  92.     {
  93.     ilbm->vp  = &ilbm->scr->ViewPort;
  94.     ilbm->srp = &ilbm->scr->RastPort;
  95.     ilbm->wrp = ilbm->win->RPort;
  96.     }
  97.     return(ilbm->win);
  98.     }
  99.  
  100.  
  101. /* Under 2.0, returns result of CloseScreen (TRUE for successful close)
  102.  * Under 1.3, always returns TRUE
  103.  */
  104. BOOL closedisplay(struct ILBMInfo *ilbm)
  105.     {
  106.     extern struct Library *IntuitionBase;
  107.     BOOL temp, result = TRUE;
  108.  
  109.     if(ilbm)
  110.     {
  111.     if(ilbm->scr)    ScreenToBack(ilbm->scr);
  112.         if(ilbm->win)    CloseWindow(ilbm->win), ilbm->win=NULL, ilbm->wrp=NULL;
  113.     if(ilbm->scr)
  114.         {
  115.         if(ilbm->scr->Flags & PUBLICSCREEN)
  116.         {
  117.         if(((struct Library *)IntuitionBase)->lib_Version >= 36)
  118.             PubScreenStatus(ilbm->scr, PSNF_PRIVATE);
  119.         }
  120.         temp = CloseScreen(ilbm->scr);
  121.         if(IntuitionBase->lib_Version >= 36)  result = temp;
  122.         if(result)
  123.         {
  124.         ilbm->scr = NULL;    
  125.             ilbm->vp  = NULL;
  126.             ilbm->srp = NULL;
  127.         }
  128.         }
  129.     }
  130.     return(result);
  131.     }
  132.  
  133.  
  134.  
  135. /* openidscreen - ILBMInfo, dimensions, modeID
  136.  *
  137.  *    Attempts to open correct 2.0 modeID screen with centered
  138.  *    overscan based on user's prefs,
  139.  *    else old 1.3 mode screen.
  140.  *
  141.  * If ilbm->stype includes CUSTOMBITMAP, ilbm->brbitmap will be
  142.  *       used as the screen's bitmap.
  143.  * If ilbm->stags is non-NULL, these tags will be added to the
  144.  *    end of the taglist.
  145.  *
  146.  * If ilbm->IFFPFlags IFFPF_USERMODE, use ilbm->usermodeid
  147.  * If ilbm->IFFPFlags IFFPF_BESTFIT, call modefallback()
  148.  *
  149.  * Returns *screen or NULL.
  150.  */
  151.  
  152. struct Screen *openidscreen(struct ILBMInfo *ilbm,
  153.                 SHORT wide, SHORT high, SHORT deep,
  154.                 ULONG mode)
  155.     {
  156.     struct NewScreen ns;            /* for old style OpenScreen */
  157.     DisplayInfoHandle displayhandle;
  158.     struct DimensionInfo dimensioninfo;
  159.     struct Rectangle spos, dclip, txto, stdo, maxo, uclip;  /* display rectangles */
  160.     struct Rectangle *uclipp;
  161.     struct Screen   *scr = NULL;
  162.     LONG   error, trynew;
  163.     ULONG  bitmaptag, passedtags;
  164.     BOOL   vctl;
  165.  
  166.     if(!ilbm)    return(0L);
  167.  
  168.     if(ilbm->IFFPFlags & IFFPF_USERMODE)    mode = ilbm->usermodeid;
  169.     if(ilbm->IFFPFlags & IFFPF_BESTFIT)
  170.     {
  171.     mode = modefallback(mode,wide,high,deep);
  172.     }
  173.  
  174.     if (trynew = ((((struct Library *)GfxBase)->lib_Version >= 36)&&
  175.           (((struct Library *)IntuitionBase)->lib_Version >= 36)))
  176.     {
  177.         /* if >= v36, see if mode is available */
  178.     if(error = ModeNotAvailable(mode))
  179.         {
  180.         D(bug("Mode $%08lx not available, error=%ld:\n",mode,error));
  181.         /* if not available, try fall back mode */
  182.         mode = modefallback(mode,wide,high,deep);
  183.         error = ModeNotAvailable(mode);
  184.  
  185.         D(bug("$%08lx ModeNotAvailable=%ld:\n",mode,error));
  186.         }
  187.  
  188.  
  189.     if(!error)    /* mode is available - how about depth ? */
  190.         {
  191.             if(displayhandle=FindDisplayInfo(mode))
  192.         {
  193.                 if(GetDisplayInfoData(displayhandle,(UBYTE *) &dimensioninfo,
  194.                 sizeof(struct DimensionInfo),DTAG_DIMS,NULL))
  195.             {
  196.             if(dimensioninfo.MaxDepth < deep)
  197.             {
  198.             message("%s: %ld %s\n",
  199.                 ilbm->ParseInfo.filename,
  200.                 ilbm->Bmhd.nPlanes,SI(MSG_ILBM_TOODEEP));
  201.             deep = dimensioninfo.MaxDepth;
  202.             }
  203.             }
  204.         }
  205.         }
  206.  
  207.     if(error) trynew = FALSE;
  208.     else trynew=((QueryOverscan(mode,&txto,OSCAN_TEXT))&&
  209.             (QueryOverscan(mode,&stdo,OSCAN_STANDARD))&&
  210.                 (QueryOverscan(mode,&maxo,OSCAN_MAX)));
  211.     }
  212.  
  213.     D(bug("\nILBM: w=%ld, h=%ld, d=%ld, mode=0x%08lx\n",
  214.         wide,high,deep,mode));    
  215.     D(bug("OPEN: %s.\n",
  216.         trynew    ? "Is >= 2.0 and mode available, trying OpenScreenTags"
  217.         : "Not 2.0, doing old OpenScreen"));
  218.  
  219.     if(trynew)
  220.     {
  221.     /* If user clip type specified and available, use it */
  222.     if(ilbm->Video) ilbm->ucliptype = OSCAN_VIDEO;
  223.     if((ilbm->ucliptype)&&(QueryOverscan(mode,&uclip,ilbm->ucliptype)))
  224.         uclipp = &uclip;
  225.     else uclipp = NULL;
  226.  
  227.     clipit(wide,high,&spos,&dclip,&txto,&stdo,&maxo,uclipp);
  228.  
  229.     D(bug("Using dclip  %ld,%ld  to  %ld,%ld... width=%ld height=%ld\n",
  230.             dclip.MinX,dclip.MinY,dclip.MaxX,dclip.MaxY,
  231.             dclip.MaxX-dclip.MinX+1,dclip.MaxY-dclip.MinY+1));
  232.     D(bug("spos->minx = %ld, spos->miny = %ld\n",spos.MinX,spos.MinY));
  233.     D(bug("DEBUG: About to attempt OpenScreenTags\n"));
  234.  
  235.     bitmaptag = ((ilbm->brbitmap)&&(ilbm->stype & CUSTOMBITMAP)) ?
  236.         SA_BitMap : TAG_IGNORE;
  237.     passedtags = ilbm->stags ? TAG_MORE : TAG_IGNORE;
  238.  
  239.     scr=(struct Screen *)OpenScreenTags((struct NewScreen *)NULL,
  240.         SA_DisplayID,    mode,
  241.         SA_Type,    ilbm->stype,
  242.         SA_Behind,    TRUE,
  243.         SA_Top,        spos.MinY,
  244.         SA_Left,    spos.MinX,
  245.         SA_Width,    wide,
  246.         SA_Height,    high,
  247.         SA_Depth,    deep,
  248.         SA_DClip,    &dclip,
  249.         SA_AutoScroll,    ilbm->Autoscroll ? TRUE : FALSE,
  250.         SA_Title,    ilbm->stitle,
  251.         SA_Font,    &SafeFont,
  252.         SA_Pens,    penarray,
  253.         SA_ErrorCode,    &error,
  254.         bitmaptag,    ilbm->brbitmap,
  255.         passedtags,    ilbm->stags,
  256.         TAG_DONE
  257.         );
  258.  
  259.         D(bug("DEBUG: OpenScreenTags scr at 0x%lx\n",scr));
  260.  
  261.         if(scr)
  262.         {
  263.         /* If caller specified a public screen, open for business */
  264.         if(scr->Flags & PUBLICSCREEN)
  265.             {
  266.             if(((struct Library *)IntuitionBase)->lib_Version >= 36)
  267.                 PubScreenStatus(scr,NULL);
  268.             }
  269.         if(ilbm->Notransb)
  270.             {
  271.             vctl=VideoControlTags(scr->ViewPort.ColorMap,
  272.                 VTAG_BORDERNOTRANS_SET, TRUE,
  273.                 TAG_DONE);
  274.  
  275.     D(bug("VideoControl to set bordernotrans, error = %ld\n",vctl));
  276.  
  277.             MakeScreen(scr);
  278.             RethinkDisplay();
  279.             }
  280.         }
  281.         else message("%s\n",openScreenErr(error));
  282.         }
  283.  
  284.     if(!scr)
  285.     {
  286.     /* ns initialization for 1.3 old style OpenScreen only
  287.          */
  288.     ns.LeftEdge = ns.TopEdge = 0;   
  289.     ns.Width     =    wide;
  290.     ns.Height     =    high;
  291.     ns.Depth    =    deep;
  292.     ns.ViewModes    =     modefallback(mode,wide,high,deep);
  293.     ns.DetailPen    =    0;
  294.     ns.BlockPen    =    1;
  295.     ns.Gadgets    =    NULL;
  296.     ns.CustomBitMap    =    ((ilbm->brbitmap)&&(ilbm->stype & CUSTOMBITMAP))
  297.                     ? ilbm->brbitmap : NULL;
  298.     ns.Font        =    &SafeFont;
  299.     ns.DefaultTitle =     ilbm->stitle;
  300.     ns.Type        =    ilbm->stype & 0x01FF;  /* allow only 1.3 types */
  301.  
  302.     scr=(struct Screen *)OpenScreen(&ns);
  303.  
  304.     D(bug("DEBUG: ns.ViewModes=0x%lx, vp->Modes=0x%lx\n",
  305.                  ns.ViewModes,scr->ViewPort.Modes));
  306.     D(bug("DEBUG: non-extended scr at 0x%lx (0=failure)\n",scr));
  307.     }
  308.     return(scr);
  309.     }
  310.  
  311.  
  312. /*
  313.  * modefallback - passed a mode id, attempts to provide a
  314.  *                suitable old mode to use instead
  315.  */
  316.  
  317. /* for old 1.3 screens */
  318. #define MODE_ID_MASK (LACE|HIRES|HAM|EXTRA_HALFBRITE)
  319.  
  320. ULONG modefallback(ULONG oldmode, SHORT wide, SHORT high, SHORT deep)
  321. {
  322.     ULONG newmode, bestmode;
  323.     struct TagItem tags[6];
  324.  
  325.     if(GfxBase->lib_Version >= 39)
  326.     {
  327.     tags[0].ti_Tag = BIDTAG_DIPFMustHave;
  328.     tags[0].ti_Data = (oldmode & HAM ? DIPF_IS_HAM : 0);
  329.     tags[0].ti_Data |= (oldmode & EXTRA_HALFBRITE ? DIPF_IS_EXTRAHALFBRITE : 0);
  330.     tags[1].ti_Tag = BIDTAG_NominalWidth;
  331.     tags[1].ti_Data = wide; //bmhd->XAspect; //wide;
  332.     tags[2].ti_Tag = BIDTAG_NominalHeight;
  333.     tags[2].ti_Data = high; //bmhd->YAspectHeight; //high;
  334.     tags[3].ti_Tag = BIDTAG_Depth;
  335.     tags[3].ti_Data = deep;
  336.     tags[4].ti_Tag = BIDTAG_SourceID;
  337.     tags[4].ti_Data = (FindDisplayInfo(oldmode) ? oldmode : (oldmode & (~(MONITOR_ID_MASK | SPRITES|GENLOCK_AUDIO|GENLOCK_VIDEO|VP_HIDE))));
  338.     tags[5].ti_Tag = TAG_DONE;
  339.  
  340.     if((bestmode = BestModeIDA(tags)) != INVALID_ID)
  341.         {
  342.         newmode = bestmode;
  343.         }
  344.     D(bug("Best fit ID = 0x%lx\n", newmode));
  345.     }
  346.     else
  347.     {
  348.         newmode = oldmode & MODE_ID_MASK;
  349.         D(bug("Falling back to 0x%08lx instead of 0x%08lx\n",newmode,oldmode));
  350.     }    
  351.     return(newmode);
  352. }
  353.  
  354.  
  355. /*
  356.  * clipit - passed width and height of a display, and the text, std, and
  357.  *          max overscan rectangles for the mode, clipit fills in the
  358.  *          spos (screen pos) and dclip rectangles to use in centering.
  359.  *          Centered around smallest containing user-editable oscan pref,
  360.  *          with dclip confined to legal maxoscan limits.
  361.  *          Screens which center such that their top is below text
  362.  *          oscan top, will be moved up.
  363.  *          If a non-null uclip is passed, that clip is used instead.
  364.  */
  365. void clipit(SHORT wide, SHORT high,
  366.         struct Rectangle *spos, struct Rectangle *dclip,
  367.         struct Rectangle *txto, struct Rectangle *stdo,
  368.         struct Rectangle *maxo, struct Rectangle *uclip)
  369. {
  370. struct  Rectangle *besto;
  371. SHORT    minx, maxx, miny, maxy;
  372. SHORT    txtw, txth, stdw, stdh, maxw, maxh, bestw, besth;
  373.  
  374.     /* get the txt, std and max widths and heights */
  375.     txtw = txto->MaxX - txto->MinX + 1;
  376.     txth = txto->MaxY - txto->MinY + 1;
  377.     stdw = stdo->MaxX - stdo->MinX + 1;
  378.     stdh = stdo->MaxY - stdo->MinY + 1;
  379.     maxw = maxo->MaxX - maxo->MinX + 1;
  380.     maxh = maxo->MaxY - maxo->MinY + 1;
  381.  
  382.     if((wide <= txtw)&&(high <= txth))
  383.     {
  384.     besto = txto;
  385.     bestw = txtw;
  386.     besth = txth;
  387.  
  388.     D(bug("Best clip is txto\n"));
  389.     }
  390.     else
  391.     {
  392.     besto = stdo;
  393.     bestw = stdw;
  394.     besth = stdh;
  395.  
  396.     D(bug("Best clip is stdo\n"));
  397.     }
  398.  
  399.     D(bug("TXTO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  stdw=%ld stdh=%ld\n",
  400.         txto->MinX,txto->MinY,txto->MaxX,txto->MaxY,txtw,txth));
  401.     D(bug("STDO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  stdw=%ld stdh=%ld\n",
  402.         stdo->MinX,stdo->MinY,stdo->MaxX,stdo->MaxY,stdw,stdh));
  403.     D(bug("MAXO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  maxw=%ld maxh=%ld\n",
  404.         maxo->MinX,maxo->MinY,maxo->MaxX,maxo->MaxY,maxw,maxh));
  405.  
  406.     if(uclip)
  407.     {
  408.     *dclip = *uclip;
  409.         spos->MinX = uclip->MinX;
  410.     spos->MinY = uclip->MinY;
  411.  
  412.     D(bug("UCLIP: mnx=%ld mny=%ld maxx=%ld maxy=%ld\n",
  413.             dclip->MinX,dclip->MinY,dclip->MaxX,dclip->MaxY));
  414.     }
  415.     else
  416.     {
  417.     /* CENTER the screen based on best oscan prefs
  418.      * but confine dclip within max oscan limits
  419.      *
  420.      * FIX MinX first */
  421.     spos->MinX = minx = besto->MinX - ((wide - bestw) >> 1);
  422.     maxx = wide + minx - 1;
  423.     if(maxx > maxo->MaxX)  maxx = maxo->MaxX;    /* too right */
  424.     if(minx < maxo->MinX)  minx = maxo->MinX;    /* too left  */
  425.  
  426.     D(bug("DCLIP: minx adjust from %ld to %ld\n",spos->MinX,minx));
  427.  
  428.     /* FIX MinY */
  429.     spos->MinY = miny = besto->MinY - ((high - besth) >> 1);
  430.     /* if lower than top of txto, move up */
  431.     spos->MinY = miny = MIN(spos->MinY,txto->MinY);
  432.     maxy = high + miny - 1;
  433.     if(maxy > maxo->MaxY)  maxy = maxo->MaxY;    /* too down  */
  434.     if(miny < maxo->MinY)  miny = maxo->MinY;    /* too up    */
  435.  
  436.     D(bug("DCLIP: miny adjust from %ld to %ld\n",spos->MinY,miny));
  437.  
  438.     /* SET up dclip */
  439.     dclip->MinX = minx;
  440.     dclip->MinY = miny;
  441.     dclip->MaxX = maxx;
  442.     dclip->MaxY = maxy;
  443.  
  444.     D(bug("CENTER: mnx=%ld mny=%ld maxx=%ld maxy=%ld\n",
  445.             dclip->MinX,dclip->MinY,dclip->MaxX,dclip->MaxY));
  446.     }
  447. }
  448.  
  449. /*----------------------------------------------------------------------*/
  450.  
  451. BOOL VideoControlTags(struct ColorMap *cm, ULONG tags, ...)
  452.     {
  453.     return (VideoControl(cm, (struct TagItem *)&tags));
  454.     }
  455.  
  456.  
  457. /*----------------------------------------------------------------------*/
  458.  
  459.